package com.sip.stream.utils.zfy.voice;

import android.content.Context;
import android.util.Log;

import com.sip.stream.ClientApp;

import aria.apache.commons.net.ftp.FTPReply;
import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

public class RawProvider extends AbstractBeepProvider {
    private static final String TAG = "RawProvider";
    private Queue<String> playQueue = new LinkedList();

    @Override
    public void release() {
    }

    public RawProvider() {
        Log.i(TAG, "RawProvider init");
    }

    @Override
    public void play(String action, String message) {
        if (action == null) {
            action = BeepType.START;
        }
        if (this.playQueue.isEmpty()) {
            this.playQueue.add(action);
            play(0);
            return;
        }
        this.playQueue.add(action);
    }

    public void play(int i) {
        String peek = this.playQueue.peek();
        Log.i(TAG, "play" + peek);
        if (peek == null) {
            return;
        }
        Single.just(peek).delay(i + getBeepDelay(peek), TimeUnit.MILLISECONDS).map(new Function<String, Boolean>() {
            @Override
            public Boolean apply(String str) throws Exception {
                RawProvider.this.loadOrPlaySoundPool(RawProvider.this.getSpeakIdentifier(str));
                return Boolean.TRUE;
            }
        }).subscribe(new SingleObserver<Boolean>() {
            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onSubscribe(Disposable disposable) {
            }

            @Override
            public void onSuccess(Boolean bool) {
                RawProvider.this.playQueue.poll();
                if (RawProvider.this.playQueue.size() > 0) {
                    RawProvider.this.play(1200);
                }
            }
        });
    }

    public int getSpeakIdentifier(String str) {
        Context applicationContext = ClientApp.clientApp.getApplicationContext();
        int identifier = applicationContext.getResources().getIdentifier("speak_" + str.toLowerCase(), "raw", applicationContext.getPackageName());
        return identifier == 0 ? getBeepIdentifier(str) : identifier;
    }

    protected int getBeepDelay(String str) {
        str.hashCode();
        char c = 65535;
        switch (str.hashCode()) {
            case -440880519:
                if (str.equals(BeepType.AUDIO_START)) {
                    c = 0;
                    break;
                }
                break;
            case 159734622:
                if (str.equals(BeepType.VIDEO_START)) {
                    c = 1;
                    break;
                }
                break;
            case 1040109159:
                if (str.equals(BeepType.INSERT_BATTERY)) {
                    c = 2;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                return FTPReply.UNRECOGNIZED_COMMAND;
            case 1:
            case 2:
                return 1000;
            default:
                return 0;
        }
    }
}
